import { getLngLat } from './AMapUtils.js';

export class AMapPositionPicker {
  constructor(map, option) {
    this.map = map;
    this.option = option;
    this.init();
  }
  init() {
    this.bindEvent();
  }
  bindEvent() {
    this.map.on('click', this.onMapClick, this);
  }
  unbindEvent() {
    this.map.off('click', this.onMapClick, this);
  }
  addMarker(lnglat) {
    if (!this.marker) {
      this.marker = new AMap.Marker(
        Object.assign({}, this.option, this.getMarkerOption(), {
          position: getLngLat(lnglat),
        })
      );
    }
    this.marker.setMap(this.map);
  }
  onMapClick(e) {
    this.addMarker(e.lnglat);
    this.onChange(e.lnglat);
  }
  onChange(lnglat) {
    console.log(lnglat);
  }
  getMarkerOption() {
    return {};
  }
  destroy() {
    if (this.marker) {
      this.marker.setMap(null);
    }
    this.unbindEvent();
  }
}
